<?php
GLOBAL $CFG;
GLOBAL $DB;
GLOBAL $USER;

define('AJAX_SCRIPT', true);
require_once('../../../config.php');
require_once('../lib.php');

$title = $_POST["title"];
$program = $_POST["program"];
$majorStr = $_POST["majors"];
$questions = $_POST["questions"];
$depthLO = $_POST["depthLO"];
$expireDate = $_POST["expireDate"];
$titleSum = $_POST["titleSum"];
$Sum = $_POST["Sum"];
$titleQuestionSum = $_POST["titleQuestionSum"];
$QuestionSum = $_POST["QuestionSum"];
$titleLOSum = $_POST["titleLOSum"];
$LOSum = $_POST["LOSum"];

$key = $_POST["curKey"];
$curSession = $USER->sesskey;
$mylib = new survey_management();

if($key = $curSession){
    //echo $title.$program.$majors.$questions.$depthLO.$expireDate.$titleSum.$Sum.$titleQuestionSum.$QuestionSum.$titleLOSum.$LOSum;

    echo "<center><h1>".$title."</h1></center>";
        //Summary
        echo "<div style=\"text-align:center;\" id=\"summary_block\">";
            echo "<div id=\"title_sum\"><h2>".$titleSum."</h2></div>";
            echo "<div style=\"text-align:left;padding-left:120px;padding-right:120px;\" id=\"sum\">".$Sum."</div>";
        echo "</div>";
        
        //Questions
        echo "<div style=\"text-align:center;\" id=\"question_block\">";
            echo "<div id=\"title_question\"><h2>".$titleQuestionSum."</h2></div>";
            echo "<div style=\"text-align:left;padding-left:120px;padding-right:120px;\" id=\"question_sum\">".$QuestionSum."<br/></div>";
        
        //Danh sách teacher subject
        echo "<div style=\"text-align:left;padding-left:120px;padding-right:120px;\" id=\"teachers-subjects\">";
        $subjects = $DB->get_records("subject", array("program_id"=>$program, ));
        echo "<u>".get_string('surveyedSubject','local_itusurveyplugin')."</u><br/>";
        $teacherStr = "";
        foreach($subjects as $subject){
            echo $subject->subject_id."-".$subject->subject_name_vn."-".$subject->subject_name_en."<br/>";
            
            //echo "select role, teacher, CONCAT(lastname,\" \", firstname) as fullname from mdl_teacher_subject tu, mdl_user u where tu.subject = ".$subject->id." and tu.teacher = u.id <br/>";
            $teachers = $DB->get_records_sql("select tu.role, tu.teacher, tu.subject, CONCAT(u.lastname,\" \", u.firstname) as fullname from mdl_teacher_subject tu, mdl_user u where tu.subject = ".$subject->id." and tu.teacher = u.id", array());
            $teacherStr .= "<ul>";
            foreach($teachers as $teacher){
                $teacherStr .= "<li>".$teacher->fullname;
                $teacherStr .= "(";
                if($teacher->role == 1){
                    $teacherStr .= get_string('instructor','local_itusurveyplugin');  
                }else{
                    $teacherStr .= get_string('ta','local_itusurveyplugin');
                }
                $teacherStr .= ")</li>";   
            }
            $teacherStr .= "</ul>";
        }
        echo "<br/><u>".get_string('teacherList','local_itusurveyplugin')."</u><br/>";
        echo $teacherStr;
        
        echo "</div>";
        
        //Danh sách câu hỏi
            echo "<div id=\"questions_of_survey\" style=\"text-align:left;padding-left:120px;padding-right:120px;\">";
                $question_ids = explode(",", $questions);
                $questionData = array();
                $answers = array();
                foreach($question_ids as $id){
                    $questionData = $DB->get_record_sql("select * from mdl_itu_questions q where q.id = ?", array($id));
                    echo "<strong>".$questionData->q_content."</strong><br/>";
                    
                    //answers
                    if($questionData->q_question_type == 1){
                        echo "<textarea width=\"600px\"></textarea><br/><br/>";
                    }
                    if($questionData->q_question_type == 2){
                        $answers = $DB->get_records_sql("select * from mdl_ans_ddl_def where add_question = ? order by add_order asc", array($id));
                        foreach($answers as $answer){
                            echo  "<input type=\"radio\" />".$answer->add_content;
                            echo "<br/>";   
                        }
                        echo "<br/><br/>";    
                    }
                    if($questionData->q_question_type == 3){
                        $answers = $DB->get_records_sql("select * from mdl_ans_cbx_def where acd_question = ? order by acd_order asc", array($id));
                        foreach($answers as $answer){
                            echo  "<input type=\"checkbox\" />".$answer->acd_content;
                            echo "<br/>";
                        }       
                        echo "<br/><br/>";
                    }
                    if($questionData->q_question_type == 4){
                        $answers = $DB->get_records_sql("select * from mdl_ans_scale_def where asd_question = ?", array($id));
                        foreach($answers as $answer){
                            $up = $answer->asd_upbound;
                            $down = $answer->asd_lowbound;
                            for($i = $down; $i <= $up; $i++){
                                echo  "<input type=\"radio\"/>".$i;
                            }       
                        }
                        echo "<br/><br/>";       
                    }  
                }
            echo "</div>";
        echo "</div>";
        
        //LOs              
        echo "<div style=\"text-align:center;\" id=\"lo_block\">";
            echo "<div id=\"title_lo_sum\"><h2>".$titleLOSum."</h2></div>";
            echo "<div style=\"text-align:left;padding-left:120px;padding-right:120px;\" id=\"lo_sum\">";
            echo $LOSum;
            echo "</div>";
            echo "<div id=\"los_of_survey\" style=\"text-align:left;padding-left:120px;padding-right:120px;\">";
                echo "<table border=\"1\" width=\"auto\" cellpadding=\"15px\">";
                    echo "<tr><td>Mã chuẩn đầu ra</td><td>Tiêu đề chuẩn đầu ra</td><td>I/T/U</td><td>Hoạt động giảng dạy và đánh giá</td></tr>";
                    //recursive process
                    displayChildLoDownToLev($program, '1', $depthLO, "0", $mylib);
                echo "</table>";
            echo "</div>";
        echo "</div>";
			
}
//Support function
function displayChildLoDownToLev($program, $lev, $deepest, $parentLo, $dblib){
    $childLos = $dblib->getLoOfLevel($program, $lev, $parentLo);
    foreach($childLos as $childLo){
    echo "<tr>";
            if(($lev) == $deepest){
            //echo "<td>".$childLo->lo_id."</td>";
            echo "<td>&nbsp;</td>";
            echo "<td>".$childLo->lo_name."</td>";            
                echo "<td>&nbsp;";
                    //echo "<input type='checkbox'/>I";
                    //echo "<input type='checkbox'/>T";
                    //echo "<input type='checkbox'/>U";
                echo "</td>";
                echo "<td>&nbsp;</td>";
                //echo "<td ><textarea></textarea></td>";
            }else if(($lev) < $deepest && ($dblib->hasLoChild($childLo->lo_id) <= 0)){
                echo "<td>".$childLo->lo_id."</td>";
                echo "<td>".$childLo->lo_name."</td>"; 
            }else if(($lev) < $deepest && ($dblib->hasLoChild($childLo->lo_id) > 0)){
                echo "<td>".$childLo->lo_id."</td>";
                echo "<td>".$childLo->lo_name."</td>";
                //if(($lev) < $deepest && ($dblib->hasLoChild($childLo->lo_id) > 0)){
                    displayChildLoDownToLev($program, $lev+1, $deepest, $childLo->lo_id, $dblib);    
                //}
                //echo "<td colspan='2'>&nbsp;sdafds</td>"; 
            }
    echo "</tr>";
            
    }
}

?>